﻿MiscTests = function () {
    this.Name = "Miscellaneous Tests";
};

MiscTests.prototype = new TestFixture();


MiscTests.prototype.TestDup = function () {
    var vm = new VM();
    vm.Execute("ldc.i4 10");
    vm.Execute("dup");
    var result = vm.State.Stack.Top();
    this.Assert(10, result.Value);

    vm.Execute("ldc.i4 2");
    vm.Execute("add");

    result = vm.State.Stack.Pop();
    this.Assert(12, result.Value);

    result = vm.State.Stack.Top();
    this.Assert(10, result.Value);
};

MiscTests.prototype.TestConstantLoads = function () {
    var vm = new VM();
    vm.Execute("ldc.i4.m1");
    this.Assert(-1, vm.State.Stack.Top().Value);

    vm.Execute("ldc.i4.M1");
    this.Assert(-1, vm.State.Stack.Top().Value);

    vm.Execute("ldc.i4.2");
    this.Assert(2, vm.State.Stack.Top().Value);

    vm.Execute("ldc.i4 22");
    this.Assert(22, vm.State.Stack.Top().Value);

    vm.Execute("ldc.r4 22.99");
    this.Assert(22.99, vm.State.Stack.Top().Value);

    vm.Execute("ldc.i8 1000");
    this.Assert(1000, vm.State.Stack.Top().Value);

    vm.Execute("ldc.i4.s 10");
    this.Assert(10, vm.State.Stack.Top().Value);
};

MiscTests.prototype.TestLoadArgs = function () {
    var vm = new VM();

    var item = new StackEntry();
    item.Type = "i4";
    item.Value = 5;
    vm.State.Arguments.push(item);

    item = new StackEntry();
    item.Type = "float";
    item.Value = 2.2;
    vm.State.Arguments.push(item);

    item = new StackEntry();
    item.Type = "i4";
    item.Value = 10;
    vm.State.Arguments.push(item);

    vm.Execute("ldarg.1");
    this.Assert("float", vm.State.Stack.Top().Type);
    this.Assert(2.2, vm.State.Stack.Top().Value);

    vm.Execute("ldarg 0");
    this.Assert("i4", vm.State.Stack.Top().Type);
    this.Assert(5, vm.State.Stack.Top().Value);

    vm.Execute("ldarg.s 2");
    this.Assert("i4", vm.State.Stack.Top().Type);
    this.Assert(10, vm.State.Stack.Top().Value);
};
